package com.solution.wx.bizman.my.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.solution.admin.auth.entity.UserEntity;
import com.solution.admin.auth.service.UserService;
import com.solution.admin.customer.entity.CustomerEntity;
import com.solution.admin.home.service.RedisService;
import com.solution.common.config.PublicConfig;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.string.StringUtil;
import com.solution.wx.common.constants.PublicConstants;
import com.solution.wx.core.constants.WechatConstants;
import com.solution.wx.core.entity.SNSUserEntity;
import com.solution.wx.core.util.WechatUtil;


/**
 * 商户个人中心
 * @author: solin
 * @date: 2018年1月1日 下午5:34:36
 */
@RequestMapping("/wx/bizman/my")
@Controller("wxBizmanMyController")
public class MyController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(MyController.class);
	@Autowired
	RedisService redisService;
	
	@Autowired
	UserService userService;
	
	@RequestMapping("")
	public String toMyPage(HttpServletRequest request) {
		request.setAttribute(PublicConstants.CURRENT_PAGE_KEY, PublicConstants.PAGE_MY);
		// 从cookie中获取openId
		String openId = WechatUtil.getOpenIdFromCookie(request);
		// 从redis中获取通过网页授权获取的用户信息
		String strSNSUserInfoJson = redisService.get(WechatConstants.SNS_USERINFO_KEY.concat(openId));
		SNSUserEntity snsUserInfo = JSONObject.parseObject(strSNSUserInfoJson, SNSUserEntity.class);
		// 获取商户信息
		UserEntity userInfo = userService.getUserById(this.getUserId());
		if (null == userInfo) {
			userInfo = new UserEntity();
		}
		String headImageUrl = "";
		if (StringUtil.isEmpty(userInfo.getHeadImage())) {
			headImageUrl = snsUserInfo.getHeadImgUrl();
		} else {
			headImageUrl = PublicConfig.IMG_ROOT_URL.concat(userInfo.getHeadImage());
		}
		userInfo.setHeadImage(headImageUrl);
		request.setAttribute("userInfo", userInfo);
		
		return "wx/bizman/my/my";
	}
	
	/**
	 * 跳转至修改密码页面
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2018年2月12日 下午4:38:39
	 */
	@RequestMapping("toResetPwd")
	public String toResetPwd(HttpServletRequest request) {
		try {
			UserEntity user = userService.getUserById(getUserId());
			String mobile = user.getMobile();
			request.setAttribute("mobile", mobile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "wx/bizman/my/reset_pwd";
	}
	
	/**
	 * 商户端修改密码
	 * @param request
	 * @param model
	 * @param mobile
	 * @param password
	 * @return
	 * @author xkl
	 * @date 2018年2月26日 下午3:03:11
	 */
	@RequestMapping("/modifyPwd")
	@ResponseBody
	public Map<String, Object> toModPwdPage(HttpServletRequest request, String mobile, String password) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 先获取用户信息
			UserEntity user = userService.getUserById(getUserId());
			if (null == user) {
				// 去登录
				resultMap.put(PublicStatus.RESULT_CODE, "0");
				resultMap.put(PublicStatus.RESULT_MSG, "未登录");
			}
			// 修改用户密码
			int rows = userService.resetUserPwdByMobile(user.getId(), user.getUserCode(), password, user.getPasswordSalt());
			System.out.println("修改用户密码结果：" + ((rows > 0) ? "成功" : "失败"));
			if (rows > 0) {
				resultMap.put(PublicStatus.RESULT_CODE, "1");
				resultMap.put(PublicStatus.RESULT_MSG, "成功");
			} else {
				resultMap.put(PublicStatus.RESULT_CODE, "0");
				resultMap.put(PublicStatus.RESULT_MSG, "失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
}
